package com.richard.base.net.interceptors

import com.richard.base.net.common.NetComponent
import okhttp3.Request
import okhttp3.Response

/**
 ***************************************
 * 项目名称:base
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2019/12/31     1:38 PM
 * 用途 okhttp header
 ***************************************

 */
class HeaderInterceptor : okhttp3.Interceptor {

//    private val userInfoProvider: UserInfoProvider? by lazy { KsRouterHelper.buildUserInfoProvider() as? UserInfoProvider }

    private fun headerJoinin(builder: Request.Builder): Request.Builder {

        val headerMap = NetComponent.getHeader().entries
        headerMap.forEach { entry ->
            builder.header(entry.key, entry.value)
        }
        return builder
//
//        builder.header(NetConstants.TOKEN, TokenHolder.token)
//                .header(NetConstants.DEVICE_ID, TokenHolder.deviceId)
//                .header(NetConstants.APPVERSION, NetComponent.application.versionName)
//                .header(NetConstants.ACCEPT_ENCODING, "identity")
//                .header(NetConstants.APIVER, "2.4")
//                .header(NetConstants.USERID, NetComponent.getUserId())
//                .header(NetConstants.APPID, HttpConfigManager.getAppId())
//                .header(NetConstants.CHANNELID, HttpConfigManager.getUmengChannel())
//                .header(NetConstants.CLIENTFORM, HttpConfigManager.getClientFrom())
//                .header(NetConstants.USERSOURCE, HttpConfigManager.getAppName())
//                .header(NetConstants.DEVICE, "android")
//                .header(NetConstants.PLATFORM, HttpConfigManager.getAppName())
//        if (ActivityCompat.checkSelfPermission(NetComponent.application, Manifest.permission.READ_PHONE_STATE)
//                !== PackageManager.PERMISSION_GRANTED) { //没有权限，则不去加上mcccode
//            return builder
//        }
//        NetComponent.application.telephonyManager?.run {
//            builder.header(NetConstants.MCCCODE, getMobileMCC(NetComponent.application))
//        }
//
//        return builder
    }

    override fun intercept(chain: okhttp3.Interceptor.Chain): Response {
        val request: Request =
                headerJoinin(chain.request().newBuilder())
                        .method(chain.request().method, chain.request().body)
                        .build()
        return chain.proceed(request)
    }


}